# -*- coding:utf-8 -*-
import os
import sys
import json
from decimal import Decimal
import MySQLdb
import datetime
def default(obj):
    if isinstance(obj, Decimal):
        return str(obj)
    raise TypeError
#连接数据库
dbstr=sys.argv[1]
city=sys.argv[2]
print dbstr
print city
d1 = datetime.datetime.now()
#conn=MySQLdb.connect(host='rds1tu038044m8zub4o7.mysql.rds.aliyuncs.com',user='zhuge',passwd='zhuge1116',db=dbstr,port=3306,charset="utf8")
#conn=MySQLdb.connect(host='101.200.81.152',user='zhugef_online',passwd='162af942c*$199b9dzc254b8b622c%db',db=dbstr,port=3307,charset="utf8")
conn=MySQLdb.connect(host='rds1tu038044m8zub4o7.mysql.rds.aliyuncs.com',user='zhugef_online',passwd='emd3ZW56*$FuYmFanVuZmVuZw%db',db=dbstr,port=3306,charset="utf8")
#conn=MySQLdb.connect(host='182.92.96.120',user='zhuge',passwd='zhuge1116',db=dbstr,port=3306,charset="utf8")
'''
cur = conn.cursor()
cur.execute('select count(*) from house_rent_gov')
#获取数据库中记录总条数
count_all=cur.fetchone()[0]
print count_all
#每一次获取1000条数据
page_count=1000
#count_all=100;
#page_count=10;
cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
#打开文件，准备写
city=city.strip('\r')
cur_path= os.path.dirname(os.path.abspath(sys.argv[0]))
filename = "%s/%s/house_rent_gov" % (cur_path, city)
file = open(filename, "w")
#for($i=0;$i<=($rent_gov_count/$page_count);$i++)
j=0
cur.execute('select * from house_rent_gov')
results=cur.fetchall()
print 'results=', results
print type(results)
d2 = datetime.datetime.now()
d=d2-d1
print 'timeinterval 1'
print d
for row in results:
    print 'row=', row
    file.write(json.dumps(row,default=default))
    file.write("\n")
file.close()
#for i in range(0,(count_all/page_count)+1):
#    cur.execute('select * from house_rent_gov limit %d,%d'%(i*page_count,page_count))
#    results=cur.fetchall()
#    print 'i=',i
#    for row in results:
#        j = j+1;
#        file.write(json.dumps(row,default=default))
#	file.write("\n")
#file.close()
d2 = datetime.datetime.now()
d=d2-d1
print d
'''

cur = conn.cursor()
cur.execute('select count(*) from house_rent_gov')
#获取数据库中记录总条数
count_all=cur.fetchone()[0]
print count_all
#每一次获取1000条数据
page_count=1000
#count_all=100;
#page_count=10;
cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
#打开文件，准备写
city=city.strip('\r')
cur_path= os.path.dirname(os.path.abspath(sys.argv[0]))
filename = "%s/%s/house_rent_gov" % (cur_path, city)
file = open(filename, "w")
#for($i=0;$i<=($sell_gov_count/$page_count);$i++)
j=0
cur.execute('select * from house_rent_gov')
results=cur.fetchall()
d2 = datetime.datetime.now()
d=d2-d1
print 'timeinterval 1'
print d
for row in results:
    file.write(json.dumps(row,default=default))
    file.write("\n")
file.close()
#for i in range(0,(count_all/page_count)+1):
#    cur.execute('select * from house_sell_gov limit %d,%d'%(i*page_count,page_count))
#    results=cur.fetchall()
#    print 'i=',i
#    for row in results:
#        j = j+1;
#        file.write(json.dumps(row,default=default))
#       file.write("\n")
#file.close()
d2 = datetime.datetime.now()
d=d2-d1
print d
